<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-10-19
 * Time: 15:00
 */
namespace App\Http\Biz;
use App\Http\Model\ConsoleAccount;
use App\Http\Model\ConsoleAccountRole;
use App\Http\Model\ConsoleRole;
use App\Http\Utils\Common\Consts;
use Exception;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Log;
use PDOException;

class SuppManagerBiz{

    //判断该账号是否超级管理员
    public function is_superman($supp_id,$account_id)
    {
        $query = ConsoleAccountRole::where(['account_id'=>$account_id,'account_types'=>1,'ownto_id'=>$supp_id]);
        //查看角色
        $role_ids = $query->pluck('role_id')->all();
        $intersection = array_intersect($role_ids, Consts::admin_role_supp);
        if(count($intersection) > 0){//有权限
            return true;
        }else{//无权限
            return false;
        }
    }

    //删除账号关系
    public function deleteAccountRoleBiz($supp_id,$account_id,$id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = ConsoleAccountRole::where(['account_id'=>$account_id,'account_types'=>1,'ownto_id'=>$supp_id]);
            //查看角色
            $role_ids = $query->pluck('role_id')->all();
            Log::info($role_ids);
            Log::info(Consts::admin_role_supp);
            $intersection = array_intersect($role_ids, Consts::admin_role_supp);
            Log::info($intersection);
            if(count($intersection) > 0){//有权限
                Log::info('===========');
                ConsoleAccountRole::destroy([$id]);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
        }
    }

    //更新权限
    public function updateAccountRoleBiz($supp_id,$account_id,$role_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = ConsoleAccountRole::where(['account_types'=>1,'ownto_id'=>$supp_id]);
            //查看角色
            $role_ids = $query->pluck('role_id')->all();
            $intersection = array_intersect($role_ids, Consts::admin_role_supp);
            if(count($intersection) > 0){//有权限
                $where = ['account_id'=>$account_id,'account_types'=>1,'ownto_id'=>$supp_id];
                $update = ['role_id'=>$role_id];
                ConsoleAccountRole::query()->updateOrCreate($where,$update);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
        }
    }

    //账号详情
    public function getAccountDetailByIdBiz($supp_id,$account_id,$role_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $result = ConsoleAccountRole::query()
                ->select([
                    'console_account_role.account_id','console_account_role.role_id','console_account_role.ownto_id as supp_id',
                    'console_role.name as role_name','console_account.can_delete','console_account.account'
                ])
                ->leftJoin('console_account','console_account.id','=','console_account_role.account_id')
                ->leftJoin('console_role','console_role.id','=','console_account_role.role_id')
                ->where(['console_account_role.account_id'=>$account_id,'console_account_role.account_types'=>1,'console_account_role.ownto_id'=>$supp_id,'console_account_role.role_id'=>$role_id])->first();

            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $result;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return null;
        }
    }

    //角色列表
    public function roleListBiz($supp_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = ConsoleAccountRole::where(['account_types'=>1,'ownto_id'=>$supp_id]);
            //查看角色
            $role_ids = $query->pluck('role_id')->all();
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return ConsoleRole::whereIn('id',$role_ids)->get();
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return array();
        }
    }

    //管理员账号列表
    public function managerListBiz($supp_id,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if($this->is_superman($supp_id,$account_id)){//是否有权限
                //所有管理员列表
                return ConsoleAccountRole::query()
                    ->leftJoin('console_account','console_account.id','=','console_account_role.account_id')
                    ->leftJoin('console_role','console_role.id','=','console_account_role.role_id')
                    ->where([
                        'console_account_role.account_types'=>1,
                        'console_account_role.ownto_id'=>$supp_id
                    ])->orderBy('console_role.id','asc')->get([
                        'console_account_role.account_id','console_account_role.role_id','console_account_role.ownto_id as supp_id',
                        'console_role.name as role_name','console_account.can_delete','console_account.account','console_account_role.id'
                    ]);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return [];
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return array();
        }
    }
    
    //添加账号权限
    public function addmanagerBiz($supp_id,$account_id,$account,$role_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if($this->is_superman($supp_id,$account_id)){//是否有权限
                //账号是否存在
                $console_account = ConsoleAccount::where('account',$account)->first();
                if($console_account == null){//创建新账号
                    $console_account = ConsoleAccount::create(['account'=>$account,'types'=>1,'uuid'=>uniqid()]);
                    $this->reset_password($console_account);//初始化密码
                }else{
                    if($console_account->types == 1){
                        //创建角色关系
                        $account_info = [];
                        $account_info['uuid'] = uniqid();
                        $account_info['account_id'] = $console_account->id;
                        $account_info['role_id'] = $role_id;
                        $account_info['ownto_id'] = $supp_id;
                        $account_info['account_types'] = 1;
                        $where = [
                            'account_id'=>$console_account->id,
                            'account_types' => 1,
                            'ownto_id' => $supp_id
                        ];
                        ConsoleAccountRole::query()->updateOrCreate($where, $account_info);
                    }
                }
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
        }
    }

    /**
     * 重置密码
     */
    public function reset_password($console_account, $is_save=true) {
        $psw_init = rand(100000, 999999);
        $console_account->password = bcrypt($psw_init);
        $console_account->password_init = $psw_init;
        if ($is_save) $console_account->save();
    }
}